<template>
    <el-select placeholder="请选择/Please Choose" v-model="value" collapse-tags
        @change="handleChange" clearable>
        <el-option v-for="(item, index) in items" :key="item.id" :value="item.kw">{{ item.kw }}</el-option>
    </el-select>
</template>
<script lang="ts">
import { defineComponent, onMounted, reactive, ref, toRefs } from 'vue'
import request from '/@/utils/request'

export default defineComponent({
    name: 'VpWarehouseLocationSelect',
    emits: ['selected'],
    props: {
        value: [],
        warehouseId:{
            type: String,
            default: '',
            required: true
        }
    },
    setup(props, context) {
        const state = reactive({
            loading: false,
            items: [] as any[],
        })
        const loadData = () => {
            state.loading = true
            request.get('/masterData/warehouse/location/listAll',{
                 params: {
                   mainid : (props.warehouseId || '')
                }
            }).then(res => {
                state.items = res?.data || []
            }).finally(() => {
                state.loading = false
            })
        }
        const selectRef = ref();
        onMounted(() => {
            loadData();
        })

        const handleChange = (data: any) => {
            context.emit("selected", data);
        }

        return {
            ...toRefs(state),
            ...toRefs(props),
            selectRef,
            handleChange,
        }
    }
})
</script>
